
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:single/util/toast_util.dart';
import '../entity/book_entity.dart';
import '../event/index_refresh_event.dart';
import '../hive/hive_helper.dart';
import '../http/api.dart';
import '../main.dart';

class DistributedBook extends StatefulWidget {
  const DistributedBook({Key? key}) : super(key: key);

  @override
  State<DistributedBook> createState() => _DistributedBookState();
}

class _DistributedBookState extends State<DistributedBook> {

  RxList<BookEntity> bookList = RxList();

  @override
  initState() {
    super.initState();
    bookList.addAll(bookQueryAll());

  }


  _title(){
    return Container(
        margin: EdgeInsets.only(left: 12.w,right: 12.w),
        width: double.infinity,
        height: 60.h,
        child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Positioned(
              left: 0,
              child:  InkWell(
                onTap: (){
                  Get.back();
                },
                child: Image.asset("assets/images/back.png",width:80.w, fit: BoxFit.fitWidth,),
              ),
            ),
            Text("柜内图书",style: TextStyle(color: const Color.fromRGBO(29, 125, 168, 1),fontSize: 16.sp,fontWeight: FontWeight.bold),),
          ],
        )
    );
  }


  _divider(){
    return Divider(
      height: 0.5.h, // Divider 组件高度
      indent: 4.w, // 分割线左侧间距
      endIndent: 4.w, // 分割线右侧间距
      color: const Color.fromRGBO(86, 169, 255, 1),
    );
  }

  _listLabel(){
    return Container(
        padding: EdgeInsets.only(top: 2.h,bottom: 2.h),
        decoration: const BoxDecoration(
          color: Colors.white,
        ),
        child: Row(
          // mainAxisAlignment: MainAxisAlignment.spaceAround,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _label('序号',50.w),
            _label('图书条码号',105.w),
            _label('图书名称',105.w),
            _label('状态',50.w),
            _label('操作',50.w),
          ],
        )
    );
  }

  _label(text,width){
    return Container(
      width: width,
      alignment: Alignment.center,
      child: Text(text,style: TextStyle(color: Colors.blueAccent,fontSize: 10.sp),textAlign: TextAlign.center,),
    );
  }

  _listTitle(){
    return Container(
      // width: 300.w,
      height: 20.h,
      decoration: BoxDecoration(
        color: const Color.fromRGBO(86, 169, 255, 1),
        borderRadius: BorderRadius.only(topLeft:Radius.circular(4.r),topRight: Radius.circular(4.r)),
      ),
      alignment: Alignment.center,
      child: Text('图书清单',style: TextStyle(color: Colors.white,fontSize: 10.sp,fontWeight: FontWeight.bold),),
    );
  }

  _item(text,width,{Color color = Colors.black}){
    return Container(
      width: width,
      alignment: Alignment.center,
      child: Text(text,style: TextStyle(color: color,fontSize: 8.sp),textAlign: TextAlign.center,),
    );
  }

  _listItem(list,index){
    var item = list[index];
    return Container(
        padding: EdgeInsets.only(top: 2.h,bottom: 2.h),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _item('${index+1}',50.w),
            _item('${item.no}',105.w),
            _item('${item.mainTitle}',105.w),
            _item(item.statue==0?'在架':'外借',50.w),
            Container(
              width: 50.w,
              alignment: Alignment.center,
              child: item.recommend == 1?
              InkWell(
                onTap: (){
                  setState(() {
                    item.recommend = 2;
                    bookInsert(item.no, item);
                  });
                },
                child: Image.asset('assets/images/un_recommend.png',width: 30.w,fit: BoxFit.fitWidth,),
              ):
              InkWell(
                  onTap: (){
                    setState(() {
                      item.recommend = 1;
                      bookInsert(item.no, item);
                    });
                  },
                child: Image.asset('assets/images/recommend.png',width: 30.w,fit: BoxFit.fitWidth,),
              )
            ),
          ],
        )
    );
  }

  _bList(){
    return Obx(() => Expanded(child: ListView.separated(
      itemBuilder: (context, index) {
        return _listItem(bookList,index);
      },
      itemCount: bookList.length,
      separatorBuilder: (BuildContext context, int index) {
        return Divider(
          height: 0.5.h, // Divider 组件高度
          // thickness: 5, // 分割线线宽，分割线居于 Divider 中心
          indent: 4.w, // 分割线左侧间距
          endIndent: 4.w, // 分割线右侧间距
          color: Colors.grey,
        );
      },
    )));
  }


  _listWidget(){
    return Container(
      width: 380.w,
      height: 500.h,
      margin: EdgeInsets.only(left: 12.w,right: 12.w),
      padding: EdgeInsets.only(bottom: 5.h),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(4.r)),
      ),
      child: Column(
        children: [
          _listTitle(),
          _listLabel(),
          _divider(),
          _bList(),
        ],
      ),

    );
  }

  bool isSynchronize = false;

  _action(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        InkWell(
          onTap: () async {
            // BotToast.showLoading();
            if(isSynchronize){
              toastWarn('封面同步中');
              return;

            }
            isSynchronize = true;
            toastWarn('封面同步中');
            for(var item in bookList){
              var book = await searchBook(item);
              if(book != null){
                bookInsert(book.no, book);
                eventFactory.fire(IndexRefreshEvent());
              }
            }
            isSynchronize = false;
            // BotToast.closeAllLoading();
          },
          child:  Image.asset("assets/images/synchronize.png", width:100.w, fit: BoxFit.fitWidth,),
        ),

        InkWell(
          onTap: () {
            Get.back();
          },
          child:  Image.asset("assets/images/confirm.png", width:100.w, fit: BoxFit.fitWidth,),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
            color: Color.fromRGBO(242,254, 255, 1)
        ),
        child: Column(
          children: [
            _title(),
            Align(child: _listWidget(),),
            _action()
          ],
        ),
      ),
    );
  }
}


